package com.wlz.rabbitmq.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

/**
 *  生产者
 * @author wlz
 * @date 2022-06-29  10:19 下午
 */

@RestController
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     *  helloworld 直连模式
     * @param message
     * @return
     */
    @ApiOperation(value = "helloworld模式发送接口",  notes = "直接发送到队列")
    @GetMapping(value = "/helloworldSend")
    public Object helloworldSend(String message) {
        MessageProperties properties = new MessageProperties();
        properties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        // 发消息
        rabbitTemplate.convertAndSend("wlz_hello_queue",
                new Message(message.getBytes(StandardCharsets.UTF_8),properties));
        return "wlz_hello_queue message sended : " + message;
    }

    /**
     *  工作队列模式
     * @param message
     * @return
     */
    @ApiOperation(value = "workQueue模式发送接口",  notes = "发送到所有监听该队列的消费")
    @GetMapping(value = "/workQueueSend")
    public Object workQueueSend(String message) {
        MessageProperties properties = new MessageProperties();
        properties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        // 发消息
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("wlz_work_queue_1",
                    new Message(message.getBytes(StandardCharsets.UTF_8),properties));
        }
        return "wlz_work_queue_1 message sended : " + message;
    }

    /**
     *  发布订阅 模式
     * @param message
     * @return
     */
    @ApiOperation(value = "fanout模式发送接口",  notes = "发送到fanoutExchange,消息将往该exchange下的所有queue转发")
    @GetMapping(value = "/fanoutSend")
    public Object fanoutSend(String message) {
        MessageProperties properties = new MessageProperties();
        properties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        // fanout 模式 只往exchange 里发送消息，分发到exchange 下的所有queue
        rabbitTemplate.convertAndSend("wlz_fanout_exchange","",
                new Message(message.getBytes(StandardCharsets.UTF_8),properties));
        return "wlz_fanout_exchange message sended : " + message;
    }


    /**
     *  routing 路由 模式， 交换机类型 direct
     * @param routingkey
     *  @param message
     * @return
     */
    @ApiOperation(value = "direct模式发送接口",  notes = "发送到directExchange,exchange 转发消息时，会往routingkey匹配的queue发送")
    @GetMapping(value = "/directSend")
    public Object directSend(String routingkey, String message) {

        if (null == routingkey) {
            routingkey = "wlz.aaa";
        }

        MessageProperties properties = new MessageProperties();
        properties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        // direct 模式 只往exchange 里发送消息，分发到exchange 下的所有queue
        rabbitTemplate.convertAndSend("wlz_direct_exchange",routingkey,
                new Message(message.getBytes(StandardCharsets.UTF_8),properties));
        return "wlz_direct_exchange message sended : routingkey > " + routingkey + "; message > " + message;
    }

    @ApiOperation(value = "topic模式发送接口",  notes = "发送到directExchange,exchange 转发消息时，会往routingkey匹配的queue发送,*代表一个单词，#代表多个单词")
    @GetMapping(value = "/topicSend")
    public Object topicSend(String routingkey, String message) {

        if (null == routingkey) {
            routingkey = "wlz.cccc";
        }

        MessageProperties properties = new MessageProperties();
        properties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        // topic 模式 只往exchange 里发送消息，分发到exchange 下的所有queue
        rabbitTemplate.convertAndSend("wlz_topic_exchange",routingkey,
                new Message(message.getBytes(StandardCharsets.UTF_8),properties));
        return "wlz_topic_exchange message sended : routingkey > " + routingkey + "; message > " + message;
    }



}
